package com.kotlin.novel.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.kotlin.novel.R
import com.kotlin.novel.listener.OnItemViewClickListener
import com.kotlin.novel.model.TypeModel

class RankAdapter (var context: Context, var typeModels: ArrayList<TypeModel>) : RecyclerView.Adapter<RankAdapter.ViewHolder>() {

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvType: TextView = itemView.findViewById(R.id.tv_item_rank)

    }

    lateinit var onItemViewClickListener: OnItemViewClickListener<TypeModel>

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_rank, parent, false))
    }

    override fun getItemCount(): Int {
        return typeModels.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val typeModel = typeModels[position]
        holder.tvType.text = typeModel.name
        holder.tvType.setOnClickListener { v -> v?.let { onItemViewClickListener.itemClick(it, typeModel) } }
    }

}